package com.davidhigenbottam.HeatMap.shared;

import java.io.Serializable;
import java.util.LinkedHashMap;

import com.davidhigenbottam.HeatMap.client.DataSources;
import com.google.gwt.user.client.ui.ListBox;


public class Query implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String SCHOOL_ENROLLMENT_RATIO_DATASOURCE= "SER";
	public static final String GROSS_ENROLLMENT_RATIO_DATASOURCE= "GER";
	public static final String NET_ENROLLMENT_RATIO_DATASOURCE= "NER";
	public static final String HIGHER_EDUCATION_ENROLLMENT = "HEE";
	public static final String STAFF_WITH_PHD_DATASOURCE= "SWP";	
	
	public static final String DEFAULT_DATASOURCE= HIGHER_EDUCATION_ENROLLMENT;
	
	public static LinkedHashMap<String, String> dataSources = new LinkedHashMap<String, String>();
	static{
		dataSources.put(HIGHER_EDUCATION_ENROLLMENT, "Higher Education Enrollment ("+ Query.HIGHER_EDUCATION_ENROLLMENT + ")");
		dataSources.put(STAFF_WITH_PHD_DATASOURCE, "Staff with PhD ("+ Query.STAFF_WITH_PHD_DATASOURCE + ")");
		dataSources.put(SCHOOL_ENROLLMENT_RATIO_DATASOURCE, "School Enrollment Ratio ("+ Query.SCHOOL_ENROLLMENT_RATIO_DATASOURCE + ")");
		dataSources.put(GROSS_ENROLLMENT_RATIO_DATASOURCE, "Gross Enrollment Ratio ("+ Query.GROSS_ENROLLMENT_RATIO_DATASOURCE + ")");
		dataSources.put(NET_ENROLLMENT_RATIO_DATASOURCE, "Net Enrollment Ratio ("+ Query.NET_ENROLLMENT_RATIO_DATASOURCE + ")");	
	}
	
	
	public static final String STUDY_LEVEL_ALL = "all";
	public static LinkedHashMap<String, String> studyLevels = new LinkedHashMap<String, String>();
	static{
		studyLevels.put(STUDY_LEVEL_ALL, "All");
		studyLevels.put("A", "Doctoral degree (S-3)");
		studyLevels.put("B","Master degree (S-2)");
		studyLevels.put("C","Bachelor degree (S-1)");
		studyLevels.put("D","4 year Diploma (D-4)");
		studyLevels.put("E", "3 year Diploma (D-3)");
		studyLevels.put("F","2 year Diploma (D-2)");
		studyLevels.put("G", "1 year Diploma (D-1)");
		studyLevels.put("H", "2 year Master for professionals");
		studyLevels.put("I","3 year Doctoral for professionals");
		studyLevels.put("J", "Profesi");
	}	
	
	public static LinkedHashMap<String, String> levelsOfEducation = new LinkedHashMap<String, String>();
	static{
		levelsOfEducation.put("1", "Elementary School");
		levelsOfEducation.put("2", "Junior High School");
		levelsOfEducation.put("3", "Senior High School");

	}	
	
	
	public static LinkedHashMap<String, String> levelsOfEducationSER = new LinkedHashMap<String, String>();
	static{
		levelsOfEducationSER.put("1", "Elementary School");
		levelsOfEducationSER.put("2", "Junior High School");
		levelsOfEducationSER.put("3", "Senior High School");
		levelsOfEducationSER.put("4", "Higher Education");
	}	
	
	public static final String INSTITUTE_TYPE_ALL = "0";
	public static LinkedHashMap<String, String> instituteTypes = new LinkedHashMap<String, String>();
	static{
		instituteTypes.put(INSTITUTE_TYPE_ALL, "All");
		instituteTypes.put("1", "University");
		instituteTypes.put("2", "Institute");
		instituteTypes.put("3", "High School");
		instituteTypes.put("4", "Academy");
		instituteTypes.put("5", "Polytechnic");
	}	
	
	public static final String INSTITUTE_OWNERSHIP_ALL = "all";
	public static final String INSTITUTE_OWNERSHIP_PUBLIC = "PTN";
	public static final String INSTITUTE_OWNERSHIP_PRIVATE = "PTS";
	
	public static LinkedHashMap<String, String> instituteOwnerships = new LinkedHashMap<String, String>();
	static{
		instituteOwnerships.put(INSTITUTE_OWNERSHIP_ALL, "All");
		instituteOwnerships.put(INSTITUTE_OWNERSHIP_PUBLIC, "Public");
		instituteOwnerships.put(INSTITUTE_OWNERSHIP_PRIVATE, "Private");
	}	

	
	//public static final String STAFF_POSITION_ALL = "all";
	
	
	public static String[] years= {"2003","2004","2005","2006","2007","2008","2009","2010","2011"};
	
	public static final String OVERLAY_PROVINCE_HEATMAP = "HEATMAP";
	public static final String OVERLAY_INSTITUTION_SYMBOL = "SYMBOL";
	public static final String OVERLAY_PROVINCE_HEATMAP_DESC = "Heatmap";
	public static final String OVERLAY_INSTITUTION_SYMBOL_DESC = "Proportional Symbol";
	
	
	public static final String DEFAULT_YEAR= "2003";
	public static final int DEFAULT_LEVEL_OF_EDUCATION= 1;
	public static final int DEFAULT_INSTITUTION_TYPE= 0;
	public static final String KML_URL= "http://studentheatmap.appspot.com/kml";
	public static final String EXCEL_URL= "http://studentheatmap.appspot.com/excel";
	
	

	
	public static final String INDONESIA_COUNTRY_CODE= "ID";

	String countryCode= INDONESIA_COUNTRY_CODE;
	String dataSource; 
	String year; 
	String levelOfEducation;
	String instituteType;
	String instituteOwnership;
	//String typeOfOverlay;
	String province;
	
	
	
	
	public Query() {
		
	}
	public Query(String dataSource, String year, String levelOfEducation, 
			String instituteType, String instituteOwnership) {

		if(isNullOrEmpty(dataSource))
			this.dataSource= SCHOOL_ENROLLMENT_RATIO_DATASOURCE;
		else
			this.dataSource = dataSource;
		
		if(isNullOrEmpty(year))
			this.year= DEFAULT_YEAR;
		else
			this.year = year;
		
		this.levelOfEducation = levelOfEducation;
		this.instituteType = instituteType;
		this.instituteOwnership = instituteOwnership;
		//this.typeOfOverlay= typeOfOverlay;
		
	}
	
	public static Query createQuery(ListBox y, ListBox l, ListBox it, ListBox io) {

		String ds= DataSources.getInstance().getDataSourceStr();
		
		String year = y.getValue(y.getSelectedIndex());
		String levelOfEducation = l.getValue(l.getSelectedIndex());
		String institutionType= "";
		if(it!=null)
			institutionType = it.getValue(it.getSelectedIndex());
		
		String instituteOwnership= io.getValue(io.getSelectedIndex());
		//String typeOfOverlay= ot.getValue(ot.getSelectedIndex());

		Query q = new Query(ds, year, levelOfEducation,institutionType,instituteOwnership);
		
		return q;
	}
	
	public String getLevelOfEducationDesc(){
		if(dataSource.equals(HIGHER_EDUCATION_ENROLLMENT)){
			return studyLevels.get(levelOfEducation);
		}
		else
			return levelsOfEducationSER.get(levelOfEducation);
		
	}
	
	
	
	public String getDescription(){

		if(isHEEnrolement()) {
			return this.dataSource+":"+ year + ":"
					+ getLevelOfEducationDesc()+ ":"
					+ instituteTypes.get(instituteType)+ ":"
					+ instituteOwnerships.get(instituteOwnership);
		}
		else if(isSchoolEnrollmentRatio() || isGrossEnrollmentRatio() ||isNetEnrollmentRatio()) {
			return this.dataSource+":"+ year + ":"
					+ getLevelOfEducationDesc();
		}
		else if(isStaffWithPhd()) {
			return this.dataSource;
		}
		else
			return "Undefined Data Source";

	}
	public String getLongDescription() {
		
		if(isHEEnrolement()) {
			return "Data Source:"+dataSources.get(this.dataSource)+"; Year:"+ year + "; Level:"
					+ getLevelOfEducationDesc()+ "; Type:"
					+ instituteTypes.get(instituteType)+ "; Ownership:"
					+ instituteOwnerships.get(instituteOwnership);
		}
		else if(isSchoolEnrollmentRatio() || isGrossEnrollmentRatio() ||isNetEnrollmentRatio()) {
			return "Data Source:"+dataSources.get(this.dataSource)+"; Year:"+ year + "; Level:"
					+ getLevelOfEducationDesc();
		}
		else if(isStaffWithPhd()) {
			return "Data Source:"+dataSources.get(this.dataSource);
		}
		else
			return "Undefined Data Source";
	}
	
	
	
	public boolean isSchoolEnrollmentDataSource(){
		return getDataSource().equals(SCHOOL_ENROLLMENT_RATIO_DATASOURCE);
	}
	
	public String getCountryCode() {
		return countryCode;
	}
	public void setCountryCode(String countryCode) {
		this.countryCode = countryCode;
	}
	public String getDataSource() {
		return dataSource;
	}
	public void setDataSource(String dataSource) {
		this.dataSource = dataSource;
	}
	public String getYear() {
		return year;
	}
	public void setYear(String year) {
		this.year = year;
	}
	public String getInstitutionType() {
		return instituteType;
	}
	
	public void setInstitutionType(String instituteType) {
		this.instituteType = instituteType;
	}
	
	
	
	public String getInstituteOwnership() {
		return instituteOwnership;
	}
	public void setInstituteOwnership(String instituteOwnership) {
		this.instituteOwnership = instituteOwnership;
	}
	public String getLevelOfEducation() {
		return levelOfEducation;
	}
	public void setLevelOfEducation(String levelOfEducation) {
		this.levelOfEducation = levelOfEducation;
	}
	

	

	public String getProvince() {
		return province;
	}
	public void setProvince(String p) {
		this.province = p;
	}
	
	public String getQueryString() {
		StringBuffer sb= new StringBuffer();
		sb.append("ds=");
		sb.append(dataSource);
		sb.append("&y=");
		sb.append(year);
		sb.append("&l=");
		sb.append(levelOfEducation);
		sb.append("&it=");
		sb.append(instituteType);
		sb.append("&io=");
		sb.append(instituteOwnership);		
		sb.append("&p=");	
		sb.append(URLParamEncoder.encode(province));	

		return sb.toString();

	}
	public String getKmlUrl() {
		String kmlUrl = KML_URL+"?"+getQueryString();
		return kmlUrl;
	}
	
	public String getExcelUrl() {
		String kmlUrl = EXCEL_URL+"?"+getQueryString();
		return kmlUrl;
	}	
	
	
	
	private boolean isNullOrEmpty(String str){
		return str == null || str.length() == 0;
	}
	
	public boolean isHEEnrolement() {
		return this.dataSource.equalsIgnoreCase(HIGHER_EDUCATION_ENROLLMENT);
	}
	
	public boolean isSchoolEnrollmentRatio() {
		return this.dataSource.equalsIgnoreCase(SCHOOL_ENROLLMENT_RATIO_DATASOURCE);
	}
	public boolean isGrossEnrollmentRatio() {
		return this.dataSource.equalsIgnoreCase(GROSS_ENROLLMENT_RATIO_DATASOURCE);
	}
	public boolean isNetEnrollmentRatio() {
		return this.dataSource.equalsIgnoreCase(NET_ENROLLMENT_RATIO_DATASOURCE);
	}
	
	public boolean isStaffWithPhd() {
		return this.dataSource.equalsIgnoreCase(STAFF_WITH_PHD_DATASOURCE);
	}
	public boolean hasProvince() {
		return province!=null;
	}
	
//	public boolean isProvinceHeatmapOverlayType(){
//		return this.typeOfOverlay.equals(OVERLAY_PROVINCE_HEATMAP);	
//	}
//	public boolean isInstitutionSymbolOverlayType(){
//		return this.typeOfOverlay.equals(OVERLAY_INSTITUTION_SYMBOL);	
//	}
		
	

	
	
	
}
